package org.shj.book1.ioblock;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class YahooFinance {

	public static double getPrice(final String ticker) throws IOException{
		final URL url = new URL("http://ichart.finance.yahoo.com/table.csv?s=" + ticker);
		final BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
		
		String discardHeader = reader.readLine();
		System.out.println(discardHeader);
		
		String data = reader.readLine();
		//Sample of data:
		//Date,Open,High,Low,Close,Volume,Adj Close
		//2016-09-05,9.08,9.12,8.98,9.02,16212600,9.02
		System.out.println(data);
		String[] dataItems = data.split(",");
		double price = Double.valueOf(dataItems[dataItems.length - 1]);
		return price;
	}
	
	
	
	public static void main(String[] args) throws IOException{
		YahooFinance.getPrice("600401.SS");
	}
}